package com.tancire.net.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.tancire.common.constant.Constants;
import com.tancire.common.core.redis.RedisCache;
import com.tancire.common.utils.DateUtils;
import com.tancire.net.domain.NetUser;
import com.tancire.net.domain.PurseInfo;
import com.tancire.net.mapper.NetUserMapper;
import com.tancire.net.service.INetUserService;
import com.tancire.net.service.IPurseInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Base64;
import java.util.List;

/**
 * 会员管理Service业务层处理
 *
 * @author chenchao
 * @date 2022-11-06
 */
@Service
public class NetUserServiceImpl implements INetUserService {

    @Resource
    private NetUserMapper netUserMapper;

    @Resource
    private RedisCache redisCache;

    @Resource
    private IPurseInfoService purseInfoService;

    /**
     * 查询会员管理
     *
     * @param id 会员管理主键
     * @return 会员管理
     */
    @Override
    public NetUser selectNetUserById(Long id) {
        return netUserMapper.selectNetUserById(id);
    }

    /**
     * 查询会员管理列表
     *
     * @param netUser 会员管理
     * @return 会员管理
     */
    @Override
    public List<NetUser> selectNetUserList(NetUser netUser) {
        return netUserMapper.selectNetUserList(netUser);
    }

    /**
     * 新增会员管理
     *
     * @param netUser 会员管理
     * @return 结果
     */
    @Override
    public int insertNetUser(NetUser netUser) {
        netUser.setCreateTime(DateUtils.getNowDate());
        return netUserMapper.insertNetUser(netUser);
    }

    /**
     * 修改会员管理
     *
     * @param netUser 会员管理
     * @return 结果
     */
    @Override
    public int updateNetUser(NetUser netUser) {
        netUser.setUpdateTime(DateUtils.getNowDate());
        // 处理数据 清空不能修改的数据
        netUser.setAccount(null);
        netUser.setPassword(null);
        netUser.setSource(null);
        netUser.setStatus(null);
        netUser.setLevel(null);
        netUser.setOpenId(null);
        netUser.setInviteCodeOut(null);
        netUser.setInviteCodeMine(null);
        int i = netUserMapper.updateNetUser(netUser);
        // 同步更新钱包信息
        this.purseInfoService.asyncUpdatePurse(netUser);
        return i;
    }

    /**
     * 批量删除会员管理
     *
     * @param ids 需要删除的会员管理主键
     * @return 结果
     */
    @Override
    public int deleteNetUserByIds(Long[] ids) {
        return netUserMapper.deleteNetUserByIds(ids);
    }

    /**
     * 删除会员管理信息
     *
     * @param id 会员管理主键
     * @return 结果
     */
    @Override
    public int deleteNetUserById(Long id) {
        return netUserMapper.deleteNetUserById(id);
    }

    @Override
    public NetUser selectNetUserByOpenId(String openid) {
        return netUserMapper.selectNetUserByOpenId(openid);
    }

    /**
     * 根据id 查询会员信息 【手机端】
     *
     * @param userId
     * @return
     */
    @Override
    public NetUser getUserInfoById(Long userId) {
        NetUser userInfo = netUserMapper.getUserInfoById(userId);
        // 加密openid
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
        String base64Str = Base64.getEncoder().encodeToString(key);
        SymmetricCrypto des = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
        String opi = des.encryptHex(userInfo.getOpenId());
        // 缓存密钥
        redisCache.setCacheObject(Constants.JM_PREFIX + opi, base64Str);
        userInfo.setOpenId(opi);
        // 获取积分信息
        PurseInfo purseInfo = this.purseInfoService.selectPurseInfoByUserId(userInfo.getId());
        if (null != purseInfo && null != purseInfo.getAccount()) {
            userInfo.setActivePoints(purseInfo.getActivePoints());
            userInfo.setCashAmount(purseInfo.getCashAmount());
        } else {
            // 初始化钱包信息
            this.purseInfoService.initPurseInfo(userInfo.getId(), userInfo.getNickname());
            userInfo.setActivePoints(BigDecimal.ZERO);
            userInfo.setCashAmount(BigDecimal.ZERO);
        }
        return userInfo;
    }

    /**
     * 更新会员头像
     *
     * @param userId
     * @param filePath
     */
    @Override
    public void updateNetUserAvatar(String userId, String filePath) {
        this.netUserMapper.updateNetUserAvatar(userId, filePath);
    }

    /**
     * 绑定邀请码
     *
     * @param code
     * @return
     */
    @Override
    public int bindInviteCode(String code) {
        return 0;
    }

    @Override
    public NetUser selectNetUserByAccount(String account) {
        return this.netUserMapper.selectNetUserByAccount(account);
    }

}
